/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import DAO.ChaveDAO;
import DAO.TimeDAO;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Chave;
import model.Mensagem;

/**
 *
 * @author Alberto
 */
public class Controller_Chave extends HttpServlet {
    public void cadastrar(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ArrayList<Object> obj = new ArrayList<Object>();
        Gson gson = new Gson();
        Mensagem msg =new Mensagem();
        Chave chave = new Chave();
        ChaveDAO dao = new ChaveDAO();
        TimeDAO timeDAO = new TimeDAO();
        try{
                    
  
            chave.setId_chave(Integer.parseInt(request.getParameter("id")));
            chave.setTime(timeDAO.buscarPorId(Integer.parseInt(request.getParameter("id_time"))));         
            dao.criar(chave);
            msg.setMensagem("Cadastro efetudado com sucesso!!!!");
            
   
            


        
        }catch(Exception e){
            System.out.println(e.getMessage());
            msg.setMensagem("Erro ao cadastrar o Jogador.");
           
            
            
        }finally{
           obj.add(msg);
            out.printf(gson.toJson(obj));
        }

    }

    public void listar(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ArrayList<Chave> obj = new ArrayList<Chave>();

        Gson objJson = new Gson();

        try {
            ChaveDAO chaveDAO = new ChaveDAO();

            obj = chaveDAO.buscarTodas(Integer.parseInt(request.getParameter("id")));
            out.print(objJson.toJson(obj));

        } catch (Exception e) {

            System.out.println("Erro na servlet ao listar os times" + e.getStackTrace());

        } finally {
            out.close();
        }

    }


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int oper = Integer.parseInt(request.getParameter("oper"));
        try {
            switch (oper) {

                case 1:
                    cadastrar(request, response);
                    break;

                case 2:
                    listar(request, response);
                    break;


            }

        } catch (Exception e) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
